package com.jec.module.sysconfig.service.convertor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jec.module.sysconfig.dao.TerminalKeyConfigRepository;
import com.jec.module.sysconfig.entity.TerminalKeyConfig;
import com.jec.module.sysmonitor.dao.TerminalDeviceRepository;
import com.jec.utils.Response;

/**
 * Created by jeremyliu on 09/10/2016.
 */
@Service
public class TerminalKeyConvertor implements Convertor<TerminalKeyConfig> {

	@Autowired
    private TerminalKeyConfigRepository terminalKeyConfigRepository;

	@Autowired
    private TerminalDeviceRepository terminalDeviceRepository;

    @Override
    public Response process(TerminalKeyConfig terminalKeyConfig) {

        Response response = Response.Builder().status(Response.STATUS_PARAM_ERROR);
        if(terminalDeviceRepository.findAllByCode(terminalKeyConfig.getDeviceNumber()).size() == 0)
            return response.message("终端号码不存在");

        if(terminalKeyConfigRepository.existsByKeyValueAndDeviceNumberAndIdNot(
        		terminalKeyConfig.getKeyValue(), 
        		terminalKeyConfig.getDeviceNumber(), 
        		terminalKeyConfig.getId()))
            return response.message("该终端该按键已配置");

        return response.status(Response.STATUS_SUCCESS);
    }

    @Override
    public Response checkBeforeRemove(int id) {
        return null;
    }
}
